*==========================================================================================================================================================
*
*	Local Projections
*	Felix Ward
*
*==========================================================================================================================================================


set more off

qui {


*-------------------------------------------------------------------------------------------------------------
*	SETTINGS
*-------------------------------------------------------------------------------------------------------------

* Variables
local vars "gdp_r_adj cpi_sil log_R_esp_er_hp itsN wage_sil "  
local lava ""Real output growth" "Consumer prices growth" "Lending rate changes" "Money inflow (% of stock)" "Nominal wage growth"" 
local yti ""Percentage pts" "Percentage pts" "Percentage pts" "Percentage pts" "Percentage pts"" 

* irf horizon
local H 5
local F 5

* rhs controls lags
local L 2

*-------------------------------------------------------------------------------------------------------------

local count = 0
foreach v of local vars {
local count = `count'+1
n display "variable `v'"

*_________ GET DATA ____________
use "`path'data/working files/prepared.dta", clear 


*____ DECLARE TIME SERIES _____
gen id =1
sort year
tsset id year, yearly


*_________________ LOCAL PROJECTIONS _________________

	***_________ IMPULSE RESPONSE and CONFIDENCE INTERVAL variables
		gen irf_`v' = . if _n<=`H'+1
		gen se_`v' = . if _n<=`H'+1

		gen ci_ub_`v' = . if _n<=`H'+1
		gen ci_lb_`v' = . if _n<=`H'+1
		gen ci_ub90_`v' = . if _n<=`H'+1
		gen ci_lb90_`v' = . if _n<=`H'+1

	***_________ ESTIMATE COEFFICIENTS
	
	forvalues y = 1/`H' {
		n display "projection `y'"
		local z = `y'+1
		local zL = `y'+`L'
		
		newey l`y'.`v'_gr  f(1/`L').`v'_gr  f(-`H'/`L').disaster_org  f(0/`L').temperature f(0/`L').warbeu f(0/`L').warbciv f(1/`L').gdp_r_adj_gr f(1/`L').cpi_sil_gr f(1/`L').wage_sil_gr f(1/`L').itsN_gr, lag(5) force 
		
		* Shock response
			lincom disaster_org 
			replace irf_`v' = r(estimate) if _n==`y' /* coefficients */
			replace se_`v' = r(se) if _n==`y'
		}			

	
replace ci_ub_`v' = irf_`v' + 1*se_`v' if _n <= `H'+1
replace ci_lb_`v' = irf_`v' - 1*se_`v' if _n <= `H'+1
replace ci_ub90_`v' = irf_`v' + 1.64*se_`v' if _n <= `H'+1
replace ci_lb90_`v' = irf_`v' - 1.64*se_`v' if _n <= `H'+1


* plot prerequisites
gen time = -_n if(_n<=`H') 
order irf* time
drop if time == . 
gen zero = 0 if time!=.

local title : word `count' of `lava'
local ytitle : word `count' of `yti'
local lsize "medlarge"
local tsize "medlarge"

twoway  (rarea ci_ub90_`v' ci_lb90_`v' time, fcolor(gs15) lcolor(gs15)) ///
		(rarea ci_ub_`v' ci_lb_`v' time, fcolor(gs12) lcolor(gs12)) ///
		(line irf_`v' time, lcolor(black) lpattern(solid) lwidth(medthick)) ///
		(line zero time, lcolor(black) lwidth(medium)), title("") xtitle("Year", size(`lsize')) ytitle(`ytitle', height(2) size(`lsize')) xlabel(,labsize(`lsize')) ylabel(, format(%9.1f) nogrid angle(0) labsize(`lsize')) ///
		legend(off) title(`title', size(`tsize')) ///
		graphregion(color(white)) plotregion(color(white)) name(g`count', replace) nodraw

} // vars

graph combine g4 g1 g3 g2 g5, name(LP_preevent, replace) cols(3) imargin(medsmall) plotregion(color(white)) graphregion(color(white)) 
graph display LP_preevent, ysize(4) xsize(6)
graph export "results/FigureB13.pdf", replace 	
	

}


*_________________________________________

			
